/* eslint-disable @typescript-eslint/restrict-plus-operands */
import EventEmitter from 'events';
import List from './List';
import {} from 'stores/video-store';
export default class VTTBuilder extends EventEmitter {
  private textTrack: TextTrack;
  private cueList: List<VTTCue>;
  constructor(video: HTMLVideoElement, lang = 'editing') {
    super();
    this.textTrack = video.addTextTrack('captions', lang);
    this.cueList = new List<VTTCue>();
    this.textTrack.mode = 'showing';
  }
  add(text: string, start: number, end: number, id = '') {
    if (this.textTrack) {
      const cue = new VTTCue(start, end, text);
      cue.id = id === '' ? this.cueList.Count.toString() : id;
      this.textTrack.addCue(cue);
      this.cueList.Add(cue);
      this.emit('added', cue);
      return cue;
    }
  }
  remove(cue: VTTCue) {
    if (this.textTrack) {
      const _cue = this.textTrack.cues?.getCueById(cue.id);
      if (_cue) {
        this.textTrack.removeCue(_cue);
      }
      this.cueList.Remove(cue);
      this.emit('removed', cue);
    }
  }
  removeById(id: string) {
    if (this.textTrack) {
      const cue = this.textTrack.cues?.getCueById(id) as VTTCue;
      if (cue) {
        this.textTrack.removeCue(cue);
        this.cueList.Remove(cue);
        this.emit('removed', cue);
      }
    }
  }
  updateText(id: string, value: string) {
    if (this.textTrack) {
      const cue = this.textTrack.cues?.getCueById(id) as VTTCue;
      if (cue) {
        cue.text = value;
      }
    }
  }
  getTrack() {
    return this.textTrack;
  }
  exportToBlob() {
    if (!this.textTrack) {
      return;
    }
    let content = '';

    if (this.cueList.Count > 0) {
      content += 'WEBVTT\n\n';
      this.cueList.getArray().forEach((cue) => {
        const start = this.formatTime(cue.startTime);
        const end = this.formatTime(cue.endTime);
        content += `${start}  -->  ${end}\n`;
        content += `${cue.text}\n`;
        content += '\n\n';
      });
    }
    if (content !== '') {
      this.emit('exported', content);
      this.download(content);
    }
  }
  pad2(number: number) {
    return (number < 10 ? '0' : '') + number;
  }

  pad3(number: number) {
    return number >= 100 ? number : '0' + this.pad2(number);
  }
  formatTime(time: number) {
    let hours = 0;
    let minutes = 0;
    let seconds = 0;
    let milliseconds = 0;
    while (time >= 60 * 60) {
      hours++;
      time -= 60 * 60;
    }
    while (time >= 60) {
      minutes++;
      time -= 60;
    }
    while (time >= 1) {
      seconds++;
      time -= 1;
    }
    milliseconds = parseInt((time * 1000).toFixed(0));
    return (
      this.pad2(hours) +
      ':' +
      this.pad2(minutes) +
      ':' +
      this.pad2(seconds) +
      '.' +
      this.pad3(milliseconds)
    );
  }
  download(content: string) {
    const aElement = document.createElement('a');
    aElement.setAttribute(
      'href',
      'data:text/plain;charset=utf-8,' + encodeURIComponent(content)
    );
    aElement.setAttribute('download', 'caption.vtt');
    aElement.click();
  }
}
